package com.linxz.wandnroid.presenter;



import com.linxz.framework.base.ActivityLifeCycleEvent;
import com.linxz.framework.base.presenter.BasePresenter;
import com.linxz.framework.protocol.callback.ModelCallBack;
import com.linxz.wandnroid.appui.view.ArticleView;
import com.linxz.wandnroid.module.WanAndroidModule;
import com.linxz.wandnroid.pojo.BannerAndArticles;
import com.linxz.wandnroid.pojo.WanAndroidArticleBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.subjects.PublishSubject;

/**
 * @author Linxz
 * 创建日期：2020年02月20日 23:30
 * version：v4.5.4
 * 描述：
 */
public class ArticlePresenter extends BasePresenter<ArticleView, WanAndroidModule> {

    private int page=0;
    public List<WanAndroidArticleBean> articleBeanList=new ArrayList<>();

    public ArticlePresenter(ArticleView mView, PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        super(mView, lifecycleSubject);
    }

    @Override
    public WanAndroidModule createModel(PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        return new WanAndroidModule(lifecycleSubject);
    }

    public void refreshData(){
        mModel.refreshArticle(new ModelCallBack<BannerAndArticles>() {
            @Override
            public void onSuccess(BannerAndArticles model) {
                if (model!=null && model.getArticlesBeans()!=null && model.getArticlesBeans().size()>0){
                    page=1;
                    articleBeanList.clear();
                    articleBeanList.addAll(model.getArticlesBeans());
                    mView.onLoadBannerSuccess(model.getBannerBeans());
                    mView.onLoadSuccess();
                }else{
                    mView.onLoadEmpty(articleBeanList.size()==0);
                }
            }

            @Override
            public void onErrors(String code, String msg) {
                mView.onLoadError(msg,articleBeanList.size()==0);
            }
        });
    }


    public void loadMoreData(){
        mModel.getArticleList(page, new ModelCallBack<List<WanAndroidArticleBean>>() {
            @Override
            public void onSuccess(List<WanAndroidArticleBean> model) {
                if (model.size()!=0){
                    articleBeanList.addAll(model);
                    mView.onLoadSuccess();
                    page++;
                }else{
                    mView.onLoadEmpty(articleBeanList.size()==0);
                }
            }

            @Override
            public void onErrors(String code, String msg) {
               mView.onLoadError(msg,false);
            }
        });
    }
}
